
package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cjgl.entity.Student;
import cjgl.entity.Teacher;
import service.StudentService;
import service.TeacherService;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	
	private TeacherService teacherService=new TeacherService();
	private StudentService studentService = new StudentService();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         request.setCharacterEncoding("utf-8");
 		String method = request.getParameter("method");
 		if("all".equals(method)) {
 			all(request,response);
 		}else if("add".equals(method)) {
 			add(request,response);
 		}  
         HttpSession session = request.getSession();
         String username =request.getParameter("username");
         String password = request.getParameter("pwd");
         Teacher teacher=teacherService.findByUsernameAndPassword(username,password);
         if(teacher==null) {
        	 
        	 
        	 ServletContext ctxt=getServletContext();
        	 Integer counter=(Integer) ctxt.getAttribute("counter");
        	 if(counter==null){
        		counter=0; 
        	 }
        	 counter++;
        	 
        	 Student student = studentService.stulogin(username,password);
        	 if(student==null) {
        		 response.sendRedirect("login.jsp");
        	 }else {
        		 session.setAttribute("user", student);
        		 request.getRequestDispatcher("stuMain.jsp").forward(request, response);
        	 }
         }
         else {
        	 session.setAttribute("user", teacher);
        	 response.sendRedirect("main.jsp");
         }
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("edit.jsp").forward(request, response);
		
	}

	private void all(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Teacher> list = teacherService.findAll();
		request.setAttribute("list", list);
		request.getRequestDispatcher("main.jsp").forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
